//
//  TPHomeViewController.m
//  美丽说图片浏览器
//
//  Created by 唐鹏 on 16/7/13.
//  Copyright © 2016年 tangpeng. All rights reserved.
//

#import "TPHomeViewController.h"
#import "TPHomeCellItem.h"
#import "TPHomeViewCell.h"
#import "TPPhotoBrowserViewController.h"
#import "TPPresentationViewController.h"
#import "TPPhotoBrowserAnimation.h"


@interface TPHomeViewController ()<UIViewControllerTransitioningDelegate>

/***/
@property (nonatomic,strong) NSMutableArray * shops;

/***/
@property (nonatomic,assign) CGRect startRect;
/***/
@property (nonatomic,assign) CGRect endRect;

/***/
@property (nonatomic,strong) UIImageView * imageView;

@property (nonatomic,strong) TPPhotoBrowserViewController *photo;

@end

@implementation TPHomeViewController

- (NSMutableArray *)shops{
    if (!_shops) {
        _shops = [NSMutableArray array];
    }
    return  _shops;
}

static NSString * const reuseIdentifier = @"cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadData:0];
}

- (void)loadData:(NSInteger)offset{
    TPHttpTool *tool = [TPHttpTool shareTool];
    [tool get:^(NSArray *result, NSError *error) {
        NSArray *array = [NSArray array];
        array = [TPHomeCellItem mj_objectArrayWithKeyValuesArray:result];
        [self.shops addObjectsFromArray:array];
        [self.collectionView reloadData];
    } offset:offset];
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.shops.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TPHomeViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.item = self.shops[indexPath.row];
    
    if (indexPath.row == self.shops.count - 1) {
        [self loadData:self.shops.count];
    }
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TPPhotoBrowserViewController *photo = [[TPPhotoBrowserViewController alloc] init];
    photo.shops = self.shops;
    photo.indexPath = indexPath;
    self.photo = photo;
    
//    photo.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    photo.modalPresentationStyle = UIModalPresentationCustom;
    photo.transitioningDelegate = self;
    CGRect startRect = [self startRect:indexPath];
    self.startRect = startRect;
    CGRect endRect = [self endRect:indexPath];
    self.endRect = endRect;
    UIImageView *imageView = [self getImageViewWithIndexPath:indexPath];
    self.imageView = imageView;
    //得到indexpath
//    if (self.indexPath) {
//        self.indexPath(indexPath,startRect,endRect);
//    }
    [self presentViewController:photo animated:YES completion:nil];
}

- (UIImageView *)getImageViewWithIndexPath:(NSIndexPath *)indexPath{
    TPHomeViewCell *cell = (TPHomeViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = cell.imageView.image;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    
    return imageView;
}
/**
 *  得到开始区域
 */
- (CGRect)startRect:(NSIndexPath *)indexPath{
    TPHomeViewCell *cell = (TPHomeViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    CGRect startRect = [self.collectionView convertRect:cell.frame toCoordinateSpace:[UIApplication sharedApplication].keyWindow];
    return startRect;
}
/**
 *  得到结束区域
 */
- (CGRect)endRect:(NSIndexPath *)indexPath{
    TPHomeViewCell *cell = (TPHomeViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    CGRect endRect = [self getImageViewFrame:cell.imageView.image];
    return endRect;
}
/**
 *  得到根据图片得到frame
 */
- (CGRect)getImageViewFrame:(UIImage *)image{
    CGFloat imageViewW = tp_screenWidth;
    CGFloat imageViewH = tp_screenWidth * image.size.height / image.size.width;
    CGFloat x = 0;
    CGFloat y = (tp_screenHeight - imageViewH) * 0.5;
    return CGRectMake(x, y, imageViewW, imageViewH);
}
#pragma mark <UIViewControllerTransitioningDelegate>

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    
    TPPresentationViewController *pvc = [[TPPresentationViewController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
   
    return pvc;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
    TPPhotoBrowserAnimation *animation = [[TPPhotoBrowserAnimation alloc] init];
    animation.presenting = YES;
    animation.startRect = self.startRect;
    animation.endRect = self.endRect;
    animation.imageView = self.imageView;
    return animation;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    TPPhotoBrowserAnimation *animation = [[TPPhotoBrowserAnimation alloc] init];
    animation.presenting = NO;
    animation.dimisImageView = self.photo.imageV;
    animation.startRect = [self startRect:self.photo.path];
    return animation;
}



@end
